package com.ydd.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.context.BaseContext;
import com.ydd.dto.*;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddCate;
import com.ydd.entity.YddUser;
import com.ydd.entity.YddUserData;
import com.ydd.mapper.YddArticleMapper;
import com.ydd.mapper.YddUserDataMapper;
import com.ydd.mapper.YddUserMapper;


import com.ydd.service.UserService;
import com.ydd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-05-02 22:42
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private YddArticleMapper yddArticleMapper;
    @Autowired
    private YddUserDataMapper yddUserDataMapper;
    @Autowired
    private YddUserMapper yddUserMapper;

    /**
     * 我的文章接口
     *
     * @return
     */
    @Override
    public UserArticleVO myArticle(UserArticleRequestDTO userArticleRequestDTO) {

        PageHelper.startPage(
                Integer.parseInt(userArticleRequestDTO.getPage()),
                Integer.parseInt(userArticleRequestDTO.getLimit())
        );

        Page<YddArticle> page = (Page<YddArticle>) yddArticleMapper.getArticleList();

        return UserArticleVO.builder()
                .count((int) page.getTotal())
                .totalPages(page.getPages())
                .pageSize(page.getPageSize())
                .currentPage(page.getPageNum())
                .data(page.getResult())
                .build();
    }

    /**
     * 我的收藏/点赞文章
     *
     * @param userDataListDTO
     * @return
     */
    @Override
    public ArticlePageListVO userDataList(UserDataListDTO userDataListDTO) {
        //根据用户id 和 type类型查询文章id
        LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUserData::getUserId, BaseContext.getCurrentId());
        wrapper.eq(YddUserData::getType, userDataListDTO.getType());
        List<YddUserData> yddUserData = yddUserDataMapper.selectList(wrapper);

        PageHelper.startPage(
                Integer.parseInt(userDataListDTO.getPage()),
                Integer.parseInt(userDataListDTO.getLimit())
        );

        //根据文章id查找
        List<YddArticleVO> list = yddArticleMapper.userDataList(yddUserData);
        Page<YddArticleVO> page = (Page<YddArticleVO>) list;

        //填充cateName
        for (YddArticleVO yddArticleVO : list) {
            YddCate yddCate = yddArticleMapper.getByCateId(yddArticleVO.getCateid());
            yddArticleVO.setCatename(yddCate.getCatename());
        }

        //返回
        ArticlePage build = ArticlePage.builder()
                .count(page.getTotal())
                .totalPages(page.getPages())
                .pageSize(page.getPageSize())
                .currentPage(page.getPageNum())
                .data(page.getResult())
                .build();

        //封装给list
        ArticlePageListVO articlePageListVO = new ArticlePageListVO();
        articlePageListVO.setList(build);

        return articlePageListVO;
    }


    /**
     * 登录成功回显数据
     *
     * @return
     */
    @Override
    public GetUserInfoVO getUserInfo() {

        String userId = BaseContext.getCurrentId();
        Integer articleNum = yddArticleMapper.getArticleCount(userId); //发布文章数
        Integer collectNum = yddUserDataMapper.getCollectCount(userId);//收藏数
        Integer zanNum = yddUserDataMapper.getZanCount(userId);//点赞数
        YddUser yddUser = yddUserMapper.getByYddUserId(userId);//用户信息

        return GetUserInfoVO.builder()
                .articleNum(articleNum)
                .collectNum(collectNum)
                .zanNum(zanNum)
                .yddUser(yddUser)
                .build();
    }

    /**
     * 新增文章
     *
     * @param editArticleDTO
     */
    @Override
    public void editArticle(EditArticleDTO editArticleDTO) {
        YddArticle yddArticle = new YddArticle();
        yddArticle.setId(editArticleDTO.getId());
        yddArticle.setStatus(editArticleDTO.getStatus());
        yddArticleMapper.updateArticle(yddArticle);
    }

    /**
     * 修改用户资料
     *
     * @param userUpdateDTO
     */
    @Override
    public void update(UserUpdateDTO userUpdateDTO) {
        YddUser yddUser = new YddUser();
        BeanUtils.copyProperties(userUpdateDTO, yddUser);
        yddUserMapper.updateById(yddUser);
    }

    /**
     * 发布文章
     *
     * @param addArticleDTO
     */
    @Override
    public void addArticle(AddArticleDTO addArticleDTO) {
        YddArticle yddArticle = new YddArticle();
        yddArticle.setUserId(Integer.valueOf(BaseContext.getCurrentId()));
        BeanUtils.copyProperties(addArticleDTO, yddArticle);
        yddArticleMapper.insert(yddArticle);
    }

    /**
     * 删除我的文章
     *
     * @param id
     */
    @Override
    public void delArticle(String id) {
        yddArticleMapper.deleteById(id);
    }

    /**
     * 点赞/取消点赞和收藏/取消收藏
     *
     * @param userDataHandleDTO
     */
    @Override
    public void userDataHandle(UserDataHandleDTO userDataHandleDTO) {
        //1表示收藏操作，2表示点赞操作
        Integer type = userDataHandleDTO.getType();
        //文章id
        Integer articleId = userDataHandleDTO.getArticle_id();
        //类型，add表示 点赞或收藏， del表示取消点赞或取消收藏
        String action = userDataHandleDTO.getAction();
        //用户id
        Integer userId = Integer.valueOf(BaseContext.getCurrentId());


        if (type == 1) {
            //收藏操作
            if (action.equals("add")) {
                //收藏
                yddUserDataMapper.addYddUserData(type,articleId,userId);
            } else {
                //表示取消收藏
                yddUserDataMapper.delYddUserData(articleId,type);
            }
        } else {
            //点赞操作
            if (action.equals("add")) {
                //点赞
                yddUserDataMapper.addYddUserData(type,articleId,userId);
            } else {
                //表示取消点赞
                yddUserDataMapper.delYddUserData(articleId,type);
            }
        }
    }
}
